﻿using System;
using OutdoorLib.BaseEntity.WS;
using OutdoorLib.BaseEntity.Agency;
using OutdoorLib.BaseEntity.Face;
using OutdoorLib.BaseEntity.Dictionary;

using OutdoorLib.BaseEntity.Provider;
using OutdoorLib.BaseEntity.Constraction;
using Grishko.SerilizeTools;
using OutdoorLib.BaseEntity.Infrastructure;
using OutdoorLib.BaseEntity.Filters;
using Grishko.DataTier;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

      // FillConstraction();
        //CreateAgency();
        //AddEmplList(AppEnvironment .CurrentAgency);
       // AppEnvironment.CurrentAgency.Save () ;


        Response.Redirect("App_Frame.aspx");
    }


    #region Сформировать систему
    /// <summary>
    /// Сформировать провайдера, агентсво, вспомогательные справочники
    /// </summary>
    private void CreateSystem()
    {
        //CreateProvider();  

        //FillSiteMap(AppEnvironment.CurrentProvider);

        //FillFilters(AppEnvironment.CurrentProvider);
        //AppEnvironment.CurrentProvider.Save();

        //CreateAgency();          

        //AddEmplList(AppEnvironment.CurrentAgency);        
        //wsAgency.SaveAgency();
        ////AddSurfGroup();
        //AddDiscounts();
    }

    private void CreateAgency()
    {
        //Создаем агентсво 
        AgencyEntity a = new AgencyEntity();
        a.ID = "1";
        a.Name = "Рекламное агентство 'Мышка Наружка'";
        a.Department.Name = "Центральный офис";
        a.Department.ID = "Dep_1";
        a.Settings.AgencyID  =a.ID;

        //a.Settings.DictionaryInfo.DictionaryList = new DictionaryList();

        //DictionaryCategoryItem cat = new DictionaryCategoryItem() { Name = "Новый справочник", ID = "11" };
        //TreeDictionaryItem new_d = cat.CreateTreeDictionaryItem();
        //a.Settings.DictionaryInfo.DictionaryList.Add(new_d);
        //a.Settings.DictionaryInfo.CategoryList.Add(cat);
        wsAgency.SaveAgency(a);
    }

    private void CreateProvider()
    {
        //Создаем провайдера
        ProviderEntity p = new ProviderEntity();
        FillFilters(p);


        DictionaryCategoryItem cat = new DictionaryCategoryItem() { Name = "Типы конструкций", ID = TreeDictionaryItem.TreeDictionary_ConstractionType };

        ////Создаем системные справочники        
        TreeDictionaryItem Dictionary = cat.CreateTreeDictionaryItem ();
        
        //Справочник носителей
        TreeDictionaryItem.BranchItem v = new TreeDictionaryItem.BranchItem() { ParamID = "_1", Value = "Щиты" };
        TreeDictionaryItem.BranchItem child = new TreeDictionaryItem.BranchItem() { ParamID = "_2", Value = "9Х16" };
        v.ChildNodes.Add(child);

        child = new TreeDictionaryItem.BranchItem() { ParamID = "_33", Value = "9Х20" };
        v.ChildNodes.Add(child);

        Dictionary.ChildNodes.Add(v);

        v = new TreeDictionaryItem.BranchItem() { ParamID = "_4", Value = "Сити формат" };
        child = new TreeDictionaryItem.BranchItem() { ParamID = "_5", Value = "Лавочки" };
        v.ChildNodes.Add(child);
        Dictionary.ChildNodes.Add(v);

        p.Settings.DictionaryInfo.DictionaryList.Add(Dictionary);
        p.Settings.DictionaryInfo.CategoryList.Add(cat);

        
        FillFilters(p);
        FillTypeMap(p);

        //TextBox1.Text = p.ToXml(true); 
        p.Save();

        
        p.Load();
    }

    private void AddEmplList(AgencyEntity a)
    {
        a.Department.Employees.Clear();
        //Grishko
        Employeer Empl = new Employeer() { ID = "Face_1", FirstName = "Гришко", SecondName = "Anton", LastName = "Вячеславович", Login = "Grishko" };
        Contact c = new Contact() { ContactType = EnContactType.Address, ContactValue = "Адрес Гришко" };
        Empl.ContactList.Add(c);

        Document d = new Document() { Date = DateTime.Now, Number = "00 777 3658", Registrator = "ОВД Промышленного р-на, г.Ставрополь" };
        Empl.DocumentList.Add(d);

        a.Department.Employees.Clear();
        a.Department.Employees.Add(Empl);

        foreach (EntitySingle i in AppEnvironment.Helper.GetObjectCollection("Employeer"))
        {
            if (i.ID == "Face_1") continue;
            a.Department.Employees.Add(i);
        }
    }
    
    private void FillFilters(ProviderEntity p)
    {
        
        p.FilterList.Clear();
        ////Создаем фильтры
        Filter f= new EmployeeListFilter();
        f.Caption = "Список сотрудников";
        f.ListCategory = Filter.ListEntityCategory.Provider;        
        p.FilterList.Add(f);

        
        f = new ConstractionListFilter();
        f.Caption = "Список конструкций";
        f.ListCategory = Filter.ListEntityCategory.Provider;        
        p.FilterList.Add(f);

        
     


    }

    private void FillTypeMap(ProviderEntity p)
    {     
        TypeMapItem i = new TypeMapItem() { TagNameID = "EmployeeList" };
        i.TypeName = "OutdoorLib.BaseEntity.SP.EmployeeListWhere";
        i.AssemblyName = "OutdoorLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";

        p.Settings.TypeMapItems.Add(i);  

    }

    private void FillSiteMap(ProviderEntity p)
    {
        p.Settings.SiteMapItems.Clear();
        SiteMapItem i = new SiteMapItem();
        i.Caption = "Справочники";
        i.PageKey = "DictionaryTree";
        i.URL = "SysDictionary_Frame.aspx";
        i.IsTabPage = true;
        p.Settings.SiteMapItems.Add (i);


    }

    private void AddSurfGroup()
    {
        //SurfaceGroup  d= new SurfaceGroup ();
        //d.ParamID = Guid.NewGuid().ToString();
        //d.Name ="Первая группа";
        //AppEnvironment.CurrentAgency.SurfaceGroups.Add(d);
        //wsAgency.SaveAgency();
 
    }

    private void AddDiscounts()
    {
        Discount d = new Discount();        
        d.Name = "Первая скидка";
        AppEnvironment.CurrentAgency.Discounts.Add(d);
        wsAgency.SaveAgency();

    }
    #endregion

    #region  PopulateData
    private void FillData()
    { 
         

    }

    private void FillConstraction()
    {
        ConstractionEntity c = new ConstractionEntity() { ID = "217cf905-0373-4431-8047-998af3447f58" };
        c.Load();

        for (int i = 3500; i < 4500; i++)
        {
            c.ID = Guid.NewGuid().ToString();
            c.Number = i.ToString();
            foreach (Side s in c.SideList)
            {
                s.ID = Guid.NewGuid().ToString();
            }
            c.SaveWitchChild();
        }


    }
    #endregion

}